from api import client
from common.route import *
import os
import configparser
from until.CycleServiceTime_random import *

#from common.read_config import conf
data = get_yaml_filepath(Environ='data.yaml')  # 读取config下的data数据
class CycleOrder_action(client.HttpClient):
    orderNo = {}  # 周期包订单号
    orderId = {}  # 周期包订单id
    key = {}  # 周几
    timeQuantum = {}  # 时间点
    serviceId = {}  # 计算库存的服务者列表
    data = {}  # 服务时间 年月日
    price = {}  # 订单支付金额

    def __init__(self, token):
        super().__init__()
        self.host = data['admin_host']
        '''初始化获取token'''
        config_path = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
                                      + r'\configs\token.ini')
        conf_token = configparser.ConfigParser()
        conf_token.read(config_path, encoding='utf-8-sig')
        test_token = conf_token.get('token', '{}'.format(token))
        self.token = test_token
    # 创建周期定
    def create_order(self, addressId, attrs, packageId, payType, discountId, source, productId, timeId, remark):
        """
        :param addressId: 用户地址id
        :param attrs: 周期包下单属性
        :param packageId: 周期包id
        :param payType: 支付方式
        :param discountId: 折扣id
        :param source:
        :param productId: 产品id
        :param timeId:
        :param remark: 备注
        :return: 接口返回值
        """
        url = cycle_order_create_list['url_create_order']
        url = self.get_full_url(url)
        method = cycle_order_create_list['method_1']
        body = {"addressId": addressId,
                "attrs": attrs,
                "packageId": packageId,
                "payType": payType,
                "discountId": discountId,
                "source": source,
                "productId": productId,
                "timeId": timeId,
                "remark": remark}
        result = self.send(url, method=method, body=body, token=self.token)
        # 订单号及id赋值给类属性
        self.orderNo = result['body']['orderNo']
        self.orderId = result['body']['orderId']
        return result
    # 获取周期库存
    def get_order_service(self):
        url = cycle_order_create_list['url_get_order_service']
        url = self.get_full_url(url, replace=self.orderId)
        method = cycle_order_create_list['method_2']
        result = self.send(url, method=method, token=self.token)
        # 调用随机获取可用的每周服务时间
        cycle_available_time = order_time_choose(result)
        # 类属性赋值
        self.key = cycle_available_time["time1"]
        self.timeQuantum = cycle_available_time['time2']
        self.serviceId = cycle_available_time['serviceId']
        return result

    # 选择第一次服务时间
    def get_cycle_first_time(self,frequency):
        url = cycle_order_create_list['url_get_first_time']
        url = self.get_full_url(url)
        method = cycle_order_create_list['method_1']
        body = {"packageId": self.orderId,
                "week": [{"key": self.key, "timeQuantum": self.timeQuantum}],
                "serviceIds": self.serviceId,
                "appoint": False,
                "frequency": frequency}
        result = self.send(url,method=method,body=body,token=self.token)
        # 调用获取随机的第一次服务时间
        first_service_time = first_order_time_choose(result)
        # 类属性赋值
        self.data = first_service_time['serviceTime']
        return result
    # 创建计划后创建订单
    def create_cycle_order(self,frequency):
        """
        :param frequency: 服务频次
        :return:
        """
        url = cycle_order_create_list['url_create_cycle_order']
        url = self.get_full_url(url,replace=self.orderId)
        method = cycle_order_create_list['method_1']
        body = {"type":2,
                "serviceTime":self.data,
                "appoint": False,
                "week":[{"key":self.key,"timeQuantum":self.timeQuantum}],
                "serviceIds":self.serviceId,
                "frequency":frequency}
        result = self.send(url,method=method,body=body,token=self.token)
        return result
    # 获取订单支付价格
    def get_order_price(self):
        url = cycle_order_create_list['url_get_order_price']
        url = self.get_full_url(url,replace=self.orderId)
        method = cycle_order_create_list['method_2']
        result = self.send(url,method=method,token=self.token)
        # 类属性赋值
        self.price = result['body']['totalPrice']
        return result
    # 支付订单
    def pay_order(self):
        url = cycle_order_create_list['url_pay_order']
        url = self.get_full_url(url,replace=self.orderNo)
        method = cycle_order_create_list['method_2']
        result = self.send(url,method=method,token=self.token)
        return result
    # 暂停周期包
    def stop_schedule(self):
        url = cycle_order_create_list['url_stop_schedule']
        url = self.get_full_url(url,replace=self.orderId)
        method = cycle_order_create_list['method_2']
        result = self.send(url,method=method,token=self.token)
        return result

